package com.graduation.rescuematerial.controller.applet;

import com.graduation.rescuematerial.dto.LogisticDTO;
import com.graduation.rescuematerial.service.ApplyMaterialService;
import com.graduation.rescuematerial.service.UserService;
import com.graduation.rescuematerial.utils.LogisticUtil;
import com.graduation.rescuematerial.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author:CaiShuangLian
 * @FileName:
 * @Date:Created in  2022/4/29 20:09
 * @Version:
 * @Description:TODO
 */
@RestController
@RequestMapping("/applet/order")
@Api(tags = "Applet——订单模块")
public class AppletOrderController {
    @Autowired
    private ApplyMaterialService applyMaterialService;
    @Autowired
    private UserService userService;

    @ApiOperation("5/02-01:申请物资")
    @PostMapping("/apply")
    @ResponseBody
    public RespBean applyMaterial(@RequestBody ApplyMaterialVo applyMaterialVo, HttpServletRequest request, HttpServletResponse response){
       String ticket=applyMaterialVo.getTicket();

        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null){
                applyMaterialVo.setDepartmentName(userVo.getCompanyName());
                return applyMaterialService.applyMaterial(applyMaterialVo);
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

//    @ApiOperation("5/01-01:组织查看待发货物资(列表只展示物资名字，数量，申请人和紧急程度)")
//    @PostMapping("/check/preparing")
//    @ResponseBody
//    public RespBean checkPending(@RequestBody String ticket,HttpServletRequest request, HttpServletResponse response){
//        if(ticket!=null && !ticket.isEmpty()){
//            //根据cookie获取用户信息
//            UserVo userVo=userService.getUserByCookie(ticket,request,response);
//            if(userVo!=null){
//                String companyName=userVo.getCompanyName();
//                return applyMaterialService.checkList(companyName,"preparing");
//            }
//        }
//        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
//    }

    @ApiOperation("组织查看组织内的订单")
    @PostMapping("/org")
    @ResponseBody
    public RespBean checkWay(@RequestBody AppletStatusVo appletStatusVo, HttpServletRequest request, HttpServletResponse response){
        String ticket=appletStatusVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null){
                return applyMaterialService.getAppletOrder(userVo.getCompanyName(),null,appletStatusVo.getStatus());
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("查看我的订单(包含我申请的物资和我确认收获的物资,可以通过status查看我的订单)")
    @PostMapping("/personal")
    @ResponseBody
    public RespBean getPersonalOrder(@RequestBody AppletStatusVo statusVo,HttpServletRequest request, HttpServletResponse response){
        String ticket=statusVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null){
//                String companyName=userVo.getCompanyName();
                Long phone=userVo.getUserPhone();
                return applyMaterialService.getAppletOrder(userVo.getCompanyName(),phone,statusVo.getStatus());
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("（组织）取消订单/（仓库）拒绝订单")
    @PostMapping("/update/status")
    @ResponseBody
    public RespBean updateStatus(@RequestBody UpdateOrderStatusVo statusVo,HttpServletRequest request, HttpServletResponse response ){
        //首先仓库和组织成员都可以修改物资状态
        //arrive,cancel,forbidden(over)
        // preparing,(待发货)
        // way(待收货)
        //初始状态：preparing
        //终止状态：over
        //preparing->cancel,forbidden,way
        //way->arrive
        String ticket=statusVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null){
                //获取companyType
                String companyType=userVo.getCompanyType();
                return applyMaterialService.updateStatus(companyType,statusVo.getOrderId());
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("发货")
    @PostMapping("/deliver")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public RespBean deliver(@RequestBody DeliverVo deliverVo,HttpServletRequest request, HttpServletResponse response ){
        String ticket=deliverVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null){
                Long phone=userVo.getUserPhone();
                //只有仓库才可操作该接口
                if(userVo.getCompanyType().equals("department")){
                    return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION,"非仓库不可调用该接口");
                }
                return applyMaterialService.deliver(deliverVo,userVo.getUserPhone());
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("收货")
    @PostMapping("/receiving")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public RespBean receiving(@RequestBody ReceivingVo receivingVo,HttpServletRequest request, HttpServletResponse response){
       String ticket=receivingVo.getTicket();
        if(ticket!=null && !ticket.isEmpty()){
            //根据cookie获取用户信息
            UserVo userVo=userService.getUserByCookie(ticket,request,response);
            if(userVo!=null){
                Long phone=userVo.getUserPhone();
                //只有仓库才可操作该接口
                if(userVo.getCompanyType().equals("warehouse")){
                    return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION,"非组织不可调用该接口");
                }
                return applyMaterialService.receiving(userVo.getUserPhone(),receivingVo.getId());
            }
        }
        return RespBean.error(RespBeanEnum.ILLEGAL_OPERATION);
    }

    @ApiOperation("查询物流信息")
    @GetMapping("/getLogistic")
    public RespBean getLogistic(@ModelAttribute LogisticDTO params) {
        return RespBean.success(LogisticUtil.getLogisticInfo(params));
    }




}
